// pages/detail/detail.js
import {Spu} from "../../models/spu";
import {ShoppingWay} from "../../core/enum";
import {SaleExplain} from "../../components/models/sale-explain";
import {getWindowHeightRpx} from "../../utils/system";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    spu: Object,
    showRealm: Boolean,
    orderWay: String,
    saleExplain: Array,
    h: Number
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    const pid = options.pid
    const spu = await Spu.getDetail(pid)
    const explain = await SaleExplain.getFixed()
    const windowHeight = await getWindowHeightRpx()
    const h = windowHeight - 100
    this.setData({
      spu: spu.data,
      saleExplain: explain,
      h
    })
  },

  onAddToCart(event) {
    this.setData({
      showRealm: true,
      orderWay: ShoppingWay.CART
    })
  },

  onBuy(event) {
    this.setData({
      showRealm: true,
      orderWay: ShoppingWay.BUY
    })
  },

  onGotoHome(event) {
    wx.switchTab({
      url: '/pages/home/home'
    })
  },

  onGotoCart(event) {
    wx.switchTab({
      url: '/pages/cart/cart'
    })
  },

  onSpecChange(event) {
    this.setData({
      noSpec: event.detail.noSpec,
      skuIntact: event.detail.skuIntact,
      missingKeys: event.detail.missingKeys,
      currentValues: event.detail.currentValues
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})